package com.btshop.goods.service.impl;
import com.btshop.goods.mapper.BrandMapper;
import com.btshop.goods.mapper.CategoryBrandMapper;
import com.btshop.goods.pojo.Brand;
import com.btshop.goods.pojo.BrandExample;
import com.btshop.goods.service.BrandService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import java.util.List;
import com.util.IdWorker;

/**
 * @ClassName: BrandServiceImpl
 * @description: Brand业务层接口实现类
 * @author: 李东
 * @Date: 2020-7-20
 */
@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private IdWorker idWorker;
    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private CategoryBrandMapper categoryBrandMapper;

    /**
     * 根据分类id查询品牌集合
     * @param categoryId
     * @return
     */
    @Override
    public List<Brand> findByCategoryId(Long categoryId) {
        List<Brand> brands = categoryBrandMapper.findBrandByCategoryId(categoryId);
        return brands;
    }

    /**
     * Brand条件+分页查询
     * @param brand 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<Brand> findPage(Brand brand, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        BrandExample example = createExample(brand);
        //执行搜索
        return new PageInfo<Brand>(brandMapper.selectByExample(example));
    }

    /**
     * Brand分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Brand> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<Brand>(this.buildSelectAll());
    }

    /**
     * Brand条件查询
     * @param brand
     * @return
     */
    @Override
    public List<Brand> findList(Brand brand){
        //构建查询条件
        BrandExample example = createExample(brand);
        //根据构建的条件查询数据
        return brandMapper.selectByExample(example);
    }


    /**
     * Brand构建查询对象
     * @param brand
     * @return
     */
    public BrandExample createExample(Brand brand){
        BrandExample example=new BrandExample();
        BrandExample.Criteria criteria = example.createCriteria();
        if(brand!=null){
            // 品牌id
            if(!StringUtils.isEmpty(brand.getId())){
                    criteria.andIdEqualTo(brand.getId());
            }
            // 品牌名称
            if(!StringUtils.isEmpty(brand.getName())){
                    criteria.andNameLike("%"+brand.getName()+"%");
            }
            // 品牌图片地址
            if(!StringUtils.isEmpty(brand.getImage())){
                    criteria.andImageEqualTo(brand.getImage());
            }
            // 品牌的首字母
            if(!StringUtils.isEmpty(brand.getLetter())){
                    criteria.andLetterEqualTo(brand.getLetter());
            }
            // 排序
            if(!StringUtils.isEmpty(brand.getSeq())){
                    criteria.andSeqEqualTo(brand.getSeq());
            }
            // 创建时间
            if(!StringUtils.isEmpty(brand.getCreateTime())){
                    criteria.andCreateTimeEqualTo(brand.getCreateTime());
            }
            // 更新时间
            if(!StringUtils.isEmpty(brand.getUpdateTime())){
                    criteria.andUpdateTimeEqualTo(brand.getUpdateTime());
            }
            // 乐观锁版本号
            if(!StringUtils.isEmpty(brand.getOptimistic())){
                    criteria.andOptimisticEqualTo(brand.getOptimistic());
            }
            // 备注字段
            if(!StringUtils.isEmpty(brand.getRemark())){
                    criteria.andRemarkEqualTo(brand.getRemark());
            }
        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void delete(Long id){
        brandMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改Brand
     * @param brand
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void update(Brand brand){
        brandMapper.updateByPrimaryKey(brand);
    }

    /**
     * 增加Brand
     * @param brand
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void add(Brand brand){
        //设置主键值
        brand.setId(idWorker.nextId());
        brandMapper.insert(brand);
    }

    /**
     * 根据ID查询Brand
     * @param id
     * @return
     */
    @Override
    public Brand findById(Long id){
        return  brandMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询Brand全部数据
     * @return
     */
    @Override
    public List<Brand> findAll() {
        return this.buildSelectAll();
    }

    public List<Brand> buildSelectAll(){
        return brandMapper.selectByExample(null);
    }
}
